<?php

use App\Admin\Controller\Base\RoleController;
use App\Common\Util\TP;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js\JsCode;
use Sc\Util\HtmlStructure\Html\Js\VueComponents\Temporary;
use Sc\Util\HtmlStructure\Table;


Html::create('管理员角色列表');

$table = Table::create(TP::route()->to([RoleController::class, 'listsData'])->url(), 'role');

$table->addColumns(
    Table\Column::selection(),
    Table\Column::normal('角色', 'name'),
    Table\Column::normal('描述', 'describe'),
    Table\Column::normal('创建时间', 'create_time'),
);


$table->setHeaderEvent("@primary.Refresh.刷新数据", "this.roleGetData()");

// ===================================================================
// 新页面: 默认为iframe模式, 当前使用 dialog-component
// ===================================================================

$component = Temporary::create('edit')->setContent(include __DIR__ . '/edit.sc.php');

$table->setHeaderEvent('@primary.Plus.新增',
    Table\EventHandler::window('新增')->setConfig(['width' => '500px'])->setComponent($component)
);

$table->setRowEvent('@primary.Edit.编辑',
    Table\EventHandler::window('编辑')->setConfig(['width' => '500px'])->setComponent($component)
);

$table->setRowEvent("@primary.权限设置",
    Table\EventHandler::window("权限设置{name}")->setUrl(TP::route()->to([RoleController::class, 'setPermission'], ['id' => '@id']))
);

$table->setHeaderEvent("@danger.delete.删除",
    Table\EventHandler::post(
        url: TP::route()->to([RoleController::class, 'destroy']),
        data: ['ids' => '@selection.map(v => v.id)'],
        successHandler: JsCode::create("this.\$message.success('删除成功')")->then("this.roleGetData()")
    )->confirmMessage('确认删除选中数据吗？')
        ->addLoading()
);
return $table->render();
